import { fileURLToPath, URL } from 'node:url'
import { join } from 'path';
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import { inspect } from 'node:util'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    AutoImport({
      resolvers: [
        ElementPlusResolver(),
        IconsResolver({
          // 修改Icon组件前缀，不设置则默认为i,禁用则设置为false
          prefix: 'Icon',
        })
      ],
    }),
    Components({
      resolvers: [
        ElementPlusResolver(),
        // 自动注册图标组件
        IconsResolver({
          // 指定collection，即指定为elementplus图标集ep
          enabledCollections: ['ep'],
        })
      ],
    }),
    Icons({
      autoInstall: true,
    }),
    inspect(),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url)),
      '@a': join(__dirname, './src/api'),
      '@u': join(__dirname, './src/utils'),
      '@c': join(__dirname, './src/components')
    }
  },
  server: {
    host: "0.0.0.0",
    cors: true,
    port: 8991,
    open: false, //自动打开
    proxy: {
      // 这里的ccc可乱写, 是拼接在url后面的地址 如果接口中没有统一的后缀可自定义
      // 如果有统一后缀, 如api, 直接写api即可, 也不用rewrite了
      "^/api/v1": {
        target: "http://127.0.0.1:8000", // 真实接口地址, 后端给的基地址
        changeOrigin: true, // 允许跨域
        // rewrite: (path) => path.replace(/^\/api/, ""), // 将ccc替换为空
      },
    },
  }
})